﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using EGov;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using System.Windows.Media.Imaging;
using System.Data.Linq;
using Microsoft.Phone.Tasks;

namespace EGov
{
    public partial class MainPage : PhoneApplicationPage
    {
        
        List<User> users = new List<User>();
        //public static User[] res = new User();  
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            string link5 = "http://egov047.juddy.vn/api/user";    
            WebClient client = new WebClient(); 
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);    
            client.OpenReadAsync(new Uri(link5), UriKind.Absolute);
            _Image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://egov047.juddy.vn/_serviceData/Images/77.jpg"));
            //client.OpenWriteAsync
        }
        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                var ser = new DataContractJsonSerializer(typeof(List<User>),null);
                users = (List<User>)ser.ReadObject(e.Result);
                for (int i = 0; i < users.Count ; i++)
                {
                    System.Diagnostics.Debug.WriteLine(users[i].UserName); ;
                }
                tbxtieude.Text = users[0].UserId.ToString();
                tbxsoluoc.Text = users[0].UserName;
                if (users[0].PhoneNumer == null)
                    users[0].PhoneNumer = "chua nhap thong tin";
                tbxnoidung.Text = users[0].PhoneNumer;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        PhotoChooserTask selectphoto = null;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                selectphoto = new PhotoChooserTask();
                selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
                selectphoto.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
       
        private void selectphoto_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {

                BinaryReader reader = new BinaryReader(e.ChosenPhoto);
                _Image.Source = new BitmapImage(new Uri(e.OriginalFileName));
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);
            }
        }       
        private void btDangbai_Click(object sender, RoutedEventArgs e)
        {            
            //
            string usertId = "1";
            string title = tbxtieude.Text;
            string summary = tbxsoluoc.Text;
            string description = tbxnoidung.Text;
            string latitude = tbxchieurong.Text;
            string longitude = tbxchieudai.Text;
            string priority = "11";
            string address = tbxdiachi.Text;
            string locationId="null";
            string link4 = "http://egov047.juddy.vn/api/place/insert?userId=" + usertId + "&title="+title+"&summary="+summary+"&description="+description+"&latitude="+latitude+"&longitude="+longitude+"&priority="+priority+"&address="+address+"&locationId="+locationId;
            WebClient dangbai = new WebClient();
            //dangbai.OpenReadCompleted += new OpenReadCompletedEventHandler(dangbai_OpenReadCompleted);
            //dangbai.OpenReadAsync(new Uri(link4), UriKind.Absolute);
            //var ser = new DataContractJsonSerializer(typeof(string), null);
            //string ac = (string)ser.ReadObject(e.Result);
            string placeId = "6963632d-ab38-4ee4-b348-05117f4078d0";
            string image = "";
            byte[] bytearray = null;
            using (MemoryStream ms = new MemoryStream())
            {
                if (_Image.Source == null)
                {

                }
                else
                {
                    WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)_Image.Source);
                    wbitmp.SaveJpeg(ms, 40, 40, 0, 82);
                    bytearray = ms.ToArray();
                }
            }
            image = Convert.ToBase64String(bytearray);

            string uridanghinh = "http://egov047.juddy.vn/api/place/InsertImage?placeId=" + placeId + "&img=" + image;
            WebClient client = new WebClient();
            client.Headers["Content-Type"] = "image/jpg";
            client.Headers["Accept"] = "application/json";
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(danghinh_OpenReadCompleted);
            client.OpenReadAsync(new Uri(uridanghinh), UriKind.Absolute);

        }
      
        private void dangbai_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var ser = new DataContractJsonSerializer(typeof(string), null);
            string ac = (string)ser.ReadObject(e.Result);
            string placeId = ac;
            string image = "";
            byte[] bytearray = null;
            using (MemoryStream ms = new MemoryStream())
            {
                if (_Image.Source == null)
                {

                }
                else
                {
                    WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)_Image.Source);
                    wbitmp.SaveJpeg(ms, 40, 40, 0, 82);
                    bytearray = ms.ToArray();
                }
            }
            image = Convert.ToBase64String(bytearray);
            
            string uridanghinh = "http://egov047.juddy.vn/api/place/InsertImage?placeId=" + placeId + "&img="+image;
            WebClient client = new WebClient();
            client.Headers["Content-Type"] = "image/jpg";
            client.Headers["Accept"] = "application/json";
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(danghinh_OpenReadCompleted);
            client.OpenReadAsync(new Uri(uridanghinh), UriKind.Absolute);
        }

        private void danghinh_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var ser = new DataContractJsonSerializer(typeof(string), null);
            string ac = (string)ser.ReadObject(e.Result);
            MessageBox.Show(ac);
        }      
      
    }
}
